package com.ruoyi.teaching.service.impl;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.teaching.domain.CurriculumMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.CurriculumMapper;
import com.ruoyi.teaching.domain.Curriculum;
import com.ruoyi.teaching.service.ICurriculumService;

/**
 * 课表 管理Service业务层处理
 * 
 * @author myh
 * @date 2024-03-25
 */
@Service
public class CurriculumServiceImpl implements ICurriculumService 
{
    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询课表 管理
     * 
     * @param id 课表 管理主键
     * @return 课表 管理
     */
    @Override
    public Curriculum selectCurriculumById(Long id)
    {
        return curriculumMapper.selectCurriculumById(id);
    }

    /**
     * 查询课表 管理列表
     * 
     * @param curriculum 课表 管理
     * @return 课表 管理
     */
    @Override
    public List<Curriculum> selectCurriculumList(Curriculum curriculum)
    {
        return curriculumMapper.selectCurriculumList(curriculum);
    }

    /**
     * 新增课表 管理
     * 
     * @param curriculum 课表 管理
     * @return 结果
     */
    @Override
    public int insertCurriculum(Curriculum curriculum)
    {
        return curriculumMapper.insertCurriculum(curriculum);
    }

    /**
     * 修改课表 管理
     * 
     * @param curriculum 课表 管理
     * @return 结果
     */
    @Override
    public int updateCurriculum(Curriculum curriculum)
    {
        return curriculumMapper.updateCurriculum(curriculum);
    }

    /**
     * 批量删除课表 管理
     * 
     * @param ids 需要删除的课表 管理主键
     * @return 结果
     */
    @Override
    public int deleteCurriculumByIds(Long[] ids)
    {
        return curriculumMapper.deleteCurriculumByIds(ids);
    }

    /**
     * 删除课表 管理信息
     * 
     * @param id 课表 管理主键
     * @return 结果
     */
    @Override
    public int deleteCurriculumById(Long id)
    {
        return curriculumMapper.deleteCurriculumById(id);
    }

    @Override
    public int importCurriculum(List<Curriculum> curriculumList, boolean updateSupport) {
        int result = curriculumMapper.insertCurriculumList(curriculumList);
        if (result > 0){
            List<Map> newCurriculumList = new ArrayList<>();
            for (Curriculum curriculum : curriculumList) {
                HashMap<Date, Long> dateAndTeacherId = new HashMap<>();
                dateAndTeacherId.put(curriculum.getCourseDate(), SecurityContextHolder.getUserId());
                newCurriculumList.add(dateAndTeacherId);
            }
            //TODO:调用自动排班
        }
        return curriculumMapper.insertCurriculumList(curriculumList);
    }

    @Override
    public List<CurriculumMenu> getCurriculumList() {
        Object campus = redisService.getCacheList("campus");
        ArrayList<CurriculumMenu> curriculumMenus = new ArrayList<>();
        String jsonStr = JSONUtil.toJsonStr(campus);
        List<JSONObject> list = JSONUtil.toList(jsonStr, JSONObject.class);
        int num1 = 1;
        int num2 = list.size() + 1;
        int num3 = 0;
        for (JSONObject jsonObject : list) {
            // 获取所有学科
            String jsonSubjectsStr = JSONUtil.toJsonStr(jsonObject.get("subjects"));
            List<JSONObject> subjects = JSONUtil.toList(jsonSubjectsStr, JSONObject.class);
            for (JSONObject subject : subjects) {
                num3++;
            }
        }
        num3 = num3 + num2;
        for (JSONObject jsonObject : list) {
            CurriculumMenu curriculumMenu = new CurriculumMenu();
//            curriculumMenu.setId(Long.valueOf(list.indexOf(jsonObject)+1));
            curriculumMenu.setId(Long.valueOf(num1));
            num1++;
            String campusName = String.valueOf(jsonObject.get("campusName"));
            curriculumMenu.setLabel(campusName);
            // 获取所有学科
            String jsonSubjectsStr = JSONUtil.toJsonStr(jsonObject.get("subjects"));
            List<JSONObject> subjects = JSONUtil.toList(jsonSubjectsStr, JSONObject.class);
            int num = 0;
            for (JSONObject subject : subjects) {
                CurriculumMenu curriculumMenu2 = new CurriculumMenu();
//                curriculumMenu2.setId(Long.valueOf(list.size()+subjects.indexOf(subject)+1));
                curriculumMenu2.setId(Long.valueOf(num2));

                String subjectName = String.valueOf(subject.get("name"));
                curriculumMenu2.setLabel(subjectName);
                // 用于查找班级名
                Curriculum curriculum = new Curriculum();
                curriculum.setCampusName(campusName);
                curriculum.setSubjectName(subjectName);
                List<Curriculum> curriculumList = curriculumMapper.selectCurriculumList(curriculum);
                for (Curriculum curriculum1 : curriculumList) {
                    String className = curriculum1.getClassName();
                    CurriculumMenu curriculumMenu3 = new CurriculumMenu();
//                    curriculumMenu3.setId(Long.valueOf(list.size()+subjects.size()+curriculumList.indexOf(curriculum1)+1));
                    curriculumMenu3.setId(Long.valueOf(num3));
                    curriculumMenu3.setLabel(className);
                    curriculumMenu2.getChildren().add(curriculumMenu3);
                    num3++;
                }
                curriculumMenu.getChildren().add(curriculumMenu2);
                num2++;
            }
            curriculumMenus.add(curriculumMenu);
        }
        return curriculumMenus;
    }

}
